package io.gitee.baicaixiaozhan.httpmessagewrapper;

import io.gitee.baicaixiaozhan.httpmessagewrapper.filter.IgnoreResultsWrapperFilter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.List;
import java.util.Optional;


/**
 * DESC:
 *
 * @author baicaixiaozhan
 * @since v1.0.0
 */
@ControllerAdvice
public class UnifiedResultsWrapperAdvice implements ResponseBodyAdvice<Object> {

    private final List<IgnoreResultsWrapperFilter> ignoreResultsWrapperFilterList;
    private final UnifiedResult<Object> unifiedResult;

    public UnifiedResultsWrapperAdvice(List<IgnoreResultsWrapperFilter> ignoreResultsWrapperFilterList,
                                       @Qualifier("unifiedResult") UnifiedResult<Object> unifiedResult) {
        this.ignoreResultsWrapperFilterList = ignoreResultsWrapperFilterList;
        this.unifiedResult = unifiedResult;
    }

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return Optional.ofNullable(returnType.getMethod())
                .map(method ->
                        !UnifiedResult.class.isAssignableFrom(method.getReturnType()) &&
                                (returnType.getDeclaringClass().isAnnotationPresent(RestController.class) ||
                                        returnType.getDeclaringClass().isAnnotationPresent(RequestBody.class) ||
                                        returnType.hasMethodAnnotation(RequestBody.class))
                )
                .orElse(false);
    }

    @Override
    public Object beforeBodyWrite(Object body,
                                  MethodParameter returnType,
                                  MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        if (!CollectionUtils.isEmpty(ignoreResultsWrapperFilterList)
                && ignored(body, returnType, selectedContentType, selectedConverterType, request, response)) {
            return body;
        }
        UnifiedResult<Object> result = unifiedResult.ok(body);
        if (unifiedResult instanceof HttpServletResponseResult) {
            return result.getData();
        }
        return result;
    }

    private boolean ignored(Object body,
                            MethodParameter returnType,
                            MediaType selectedContentType,
                            Class<? extends HttpMessageConverter<?>> selectedConverterType,
                            ServerHttpRequest request, ServerHttpResponse response) {
        return ignoreResultsWrapperFilterList.stream()
                .anyMatch(filter ->
                        filter.ignored(body, returnType, selectedContentType, selectedConverterType, request, response));
    }
}
